﻿using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using WMSABL;
using WMSAML.Warehouse.BLL;
using SYDZ.WMS.DBUtility;
using ElectronicWMSA.Comm.Forms.Message;
using ElectronicWMSA.Comm.ClassFiles;

namespace ElectronicWMSA.Warehouse
{
    public partial class List_StockOutGoingFrm : Form
    {
        private string strWhere1 = "";
        PublicDataSet bll_PublicDataSet = new PublicDataSet();
        public List_StockOutGoingFrm()
        {
            InitializeComponent();
        }

        private void List_StockOutGoingFrm_Load(object sender, EventArgs e)
        {
            this.pageControl1.PageCurrent = 1;
            this.pageControl1.PageSize = 50;
        }
        public void DBindCombox()
        {
            DataSet ds = bll_PublicDataSet.GetDataSet("PD_Warehouse", "");
            DataTable dt = ds.Tables[0];
            DataRow dr = dt.NewRow();
            dr["NID"] = "0";
            dr["WarehouseName"] = "--请选择--";
            dt.Rows.InsertAt(dr, 0);
        }

        #region 数据
        private int DBindData1()
        {
            Comm.Forms.MsControls.PageData pageData = new Comm.Forms.MsControls.PageData();
            pageData.TableName = "T_PickStockPrepare";
            pageData.PrimaryKey = "NID";
            pageData.OrderStr = "NID";
            pageData.PageIndex = this.pageControl1.PageCurrent;
            pageData.PageSize = this.pageControl1.PageSize;
            pageData.QueryCondition = strWhere1;
            pageData.QueryFieldName = "*";

            this.pageControl1.bindingSource.DataSource = pageData.QueryDataTable().Tables[0];
            this.pageControl1.bindingNavigator.BindingSource = pageControl1.bindingSource;
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = this.pageControl1.bindingSource;
            this.dataGridView1.ClearSelection();
            this.dataGridView1.CurrentCell = null;
            return pageData.TotalCount;
        }
        private int pageControl1_EventPaging(ElectronicWMSA.Comm.Forms.MsControls.EventPagingArg e)
        {
            return DBindData1();
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor);
            e.Graphics.DrawString((e.RowIndex + 1).ToString(System.Globalization.CultureInfo.CurrentUICulture), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
        }
        #endregion

        private void btnUserSearch_Click(object sender, EventArgs e)
        {
            if (!SystemVariable.VaildationPermission("查询")) return;
            strWhere1 = " 1=1 ";
            if (!string.IsNullOrEmpty(txt_PO.Text))
            {
                this.strWhere1 += "  AND  OutList_NO like '%" + txt_PO.Text.Trim() + "%'";
            }

            if (this.txt_PICK.Text.Trim() != "--请选择--" && this.txt_PICK.Text.Trim() != "")
                this.strWhere1 += " AND PickList_NO like '%" + this.txt_PICK.Text.Trim() + "%' ";

            if (this.txt_Machine.Text.Trim() != "--请选择--" && this.txt_Machine.Text.Trim() != "")
                this.strWhere1 += " AND Replace(Product_Model,' ','')  like  '%" + this.txt_Machine.Text.Trim() + "' ";
            if (!string.IsNullOrEmpty(txt_SN.Text))
            {
                this.strWhere1 += "  AND  Serial_Number like '" + txt_SN.Text.Trim() + "%'";
            }
            if (dtpTimeStart.Checked)
            {
                this.strWhere1 += "  AND  BK08 >= '" + dtpTimeStart.Text.Trim() + "'";

            }
            if (dtpTimeEnd.Checked)
            {
                this.strWhere1 += "  AND  BK08 <= '" + dtpTimeEnd.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(com_BH.Text))
            {
                this.strWhere1 += "  AND  BanHao like '" + com_BH.Text.Trim() + "%'";
            }
            this.pageControl1.Bind();
        }

        #region  Close
        private void linkLabelClose_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPages();
        }

        private void linkLabellinkLabelCloseAll_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseAllTabPages();
        }

        private void linkLabelCloseElse_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Main._Main.CloseTabPageElse();
        }
        #endregion

        private void CB_CheckedChanged(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (CB.Checked == true)
                {
                    dataGridView1.Rows[i].Cells[1].Value = "True";
                }
                else
                {
                    dataGridView1.Rows[i].Cells[1].Value = "False";
                }
            }
        }
        T_PickStockPrepare TBSbll = new T_PickStockPrepare();
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> list = new List<string>();
                List<string> listSpiping = new List<string>();// 删除上送数据
                int count = 0;
                string IDnum = "";
                for (int i = 0; i < dataGridView1.RowCount; i++)
                {
                    if (dataGridView1.Rows[i].Cells[1].FormattedValue.ToString() == "True")
                    {
                        if (IDnum == "")
                        {
                            IDnum = dataGridView1.Rows[i].Cells["NID"].Value.ToString();
                        }
                        else
                        {
                            IDnum += "," + dataGridView1.Rows[i].Cells["NID"].Value.ToString();
                        }
                        count++;
                    }
                }
                if (!string.IsNullOrEmpty(IDnum))
                {
                    if (MessageClass.enquire("确认撤销选择的【" + count + "】条记录？") == DialogResult.OK)
                    {
                        DataTable dt = TBSbll.GetList("  NID IN (" + IDnum + ")").Tables[0];
                        foreach (DataRow dr in dt.Rows)
                        {
                            if (string.IsNullOrEmpty(dr["Serial_Number"].ToString()))
                            {
                                list.Add(" delete  T_PickStockPrepare WHERE  " +
                                    "Product_Model='" + dr["Product_Model"].ToString() + "' and " +
                                    "Serial_Number='" + dr["Serial_Number"].ToString() + "' and " +
                                    "PickList_NO ='" + dr["PickList_NO"].ToString() + "'");

                                string sqlcount = "select bk06 from  T_Stock WHERE BK03='Option' and  BK06<>0  and  Product_Model='" + dr["Product_Model"].ToString() + "'";

                                int num = Convert.ToInt32(DbHelperSQL.GetSingle(sqlcount));

                                string sql = "  update  T_Stock set BK06='" + (num + Convert.ToInt32(dr["BK06"].ToString())) + "',BK13=NULL " +
                                    "WHERE NID in (select Min(NID) from  T_Stock " +
                                    "WHERE BK03='Option' and  BK06<>0  and  Product_Model='" + dr["Product_Model"].ToString() + "')";

                                list.Add(sql);
                            }
                            else
                            {
                                list.Add(" delete  T_PickStockPrepare WHERE  " +
                                    "Product_Model='" + dr["Product_Model"].ToString() + "' and  " +
                                    "Serial_Number='" + dr["Serial_Number"].ToString() + "' and PickList_NO ='" + dr["PickList_NO"].ToString() + "'");

                                list.Add(" update [T_Stock] set " +
                                    "[OutList_NO] = NULL ,[PickList_NO] = NULL  ,[BanHao]  = NULL ,[Create_Time] = NULL , [BK04] = NULL ,[BK06] ='1', bk11=NULL,BK13=NULL  " +
                                    "WHERE  " +
                                    "[Product_Model]='" + dr["Product_Model"].ToString() + "' and  " +
                                    "[Serial_Number]='" + dr["Serial_Number"].ToString() + "' and " +
                                    "PickList_NO ='" + dr["PickList_NO"].ToString() + "'");

                            }
                            list.Add(" Insert into  [dbo].[T_GCStock]([OutList_NO],[PickList_NO],[BanHao],[List_NOType] ,[Product_Model],[Serial_Number],[Update_UserID],[Update_Time],[Remark],[BK03], BK06 ) values(" +
                                " '" + dr["OutList_NO"].ToString() + "'," +
                                "'" + dr["PickList_NO"].ToString() + "', " +
                                "'" + dr["BanHao"].ToString() + "'," +
                                "'" + dr["List_NOType"].ToString() + "', " +
                                "'" + dr["Product_Model"].ToString() + "'," +
                                "'" + dr["Serial_Number"].ToString() + "', " +
                                "'" + SystemVariable.Login_Name + "'," +
                                "'" + dr["Update_Time"].ToString() + "'," +
                                "'" + DateTime.Now.ToString() + "', " +
                                "'出库撤消'," +
                                "'" + dr["Bk06"].ToString() + "'  )");
                        }
                        if (DbHelperSQL.ExecuteSqlTran(list) == 0)
                        {
                            MessageClass.error("操作失败！");
                        }
                        else
                        {
                            MessageClass.right("操作成功！");
                            foreach (DataRow dr in dt.Rows)
                            {
                                bll_PublicDataSet.ExecuteSQL("DELETE FROM [172.17.96.19,2433\\PROAV].TDB_Shipping.[dbo].t_ShippingRecord " +
                                    "WHERE  Modelname = '" + dr["Product_Model"].ToString().Trim() + "' AND Serial = '" + dr["Serial_Number"].ToString().Trim() + "' ");
                            }

                            SystemVariable.AddLogInfo("出库撤消", "操作员：【" + SystemVariable.Login_Name + "】操作了撤消出库操作");

                            this.pageControl1.Bind();
                        }
                    }
                }
                else
                {
                    MessageClass.warning("请选中撤销行！");
                }

            }
            catch (Exception ex)
            {
                MessageClass.error("操作失败！");
                ErrorLogLoad.ErrorLogInfo(ex.Message.ToString());
            }
        }
    }
}
